#include<iostream>
#include"LinkQueue.h"
#include"LinkStack.h"

//两个队列实现栈
//两个栈实现队列

class Queue
{
public:
    Queue():size(list2.Size()){};
    ~Queue(){}
    void Push(const ElementType &element);
    void Pop();
    ElementType& GetFront();
    bool IsEmpty();

private:
    LinkStack list1;
    LinkStack list2;
    size_t size; 


};

void Queue::Push(const ElementType &element)
{
    
    list1.Push(element);
    list2.Push(list1.GetTop());
    list1.Pop();
    size++;
}
void Queue::Pop()
{
    list2.Pop();
    size--;
}
ElementType &Queue::GetFront()
{

    return list2.GetTop();

}
bool Queue::IsEmpty()
{
    return size==0;
}

class Stack
{

public:
    Stack():size(0){};
    ~Stack();

    //入栈
    void Push(const ElementType&element);
    //出栈
    void Pop();
    //获取栈顶
    ElementType &GetTop();
    bool IsEmpty();
    size_t Size(); 


private:
    LinkQueue list1;
    LinkQueue list2;
    size_t size;

};

Stack::~Stack()
{
}

void Stack::Push(const ElementType &element)
{
    if (list1.Size()==0)
    {
        list2.Push(element);
        size++;
                
    }
    else if(list2.Size()==0)
    {
        list1.Push(element);
        size++;
    }
    else
    {

    }

}

void Stack::Pop()
{
   
        if (list1.Size()!=0)
        {  for (int i = 0; i < size-2; i++)
        {
           list2.Push(list1.GetFront());
           list1.Pop();
        }
        list1.Pop();
        size=list2.Size();
        }
        else
        {
        for (int i = 0; i < size-2; i++)
        {
        list1.Push(list2.GetFront());
        list2.Pop();
        }
        list2.Pop();
        size=list1.Size();
        }
    
}

ElementType &Stack::GetTop()
{
    if (list1.Size()!=0)
        {  for (int i = 0; i < size-2; i++)
        {
           list2.Push(list1.GetFront());
           list1.Pop();
        }
        ElementType result=list1.GetFront();
        list2.Push(list1.GetFront());
        list1.Pop();
        return list1.GetFront();
        }
        else
        {
        for (int i = 0; i < size-2; i++)
        {
        list1.Push(list2.GetFront());
        list2.Pop();
        }
        ElementType result=list2.GetFront();
        list1.Push(list2.GetFront());
        list2.Pop();
        return result;
        }
}
